package gui;

import db.DBManager;
import db.DBQuery;
import struct.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.ResultSet;
import java.awt.*;
import java.awt.print.*;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.*;
import java.lang.Math.*;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.filechooser.FileNameExtensionFilter;
import au.com.bytecode.opencsv.CSVWriter;

/**
 *
 * @author Sarah Wegner
 * @author Gabrielle DuBois
 *
 */
public class MainGui extends javax.swing.JFrame {

    /** Variables*/
    Object[] editRecord;//record selected, and being edited
    boolean searchWithinClicked = false;//Boolean for custom search button clicked
    boolean searchPredefinedClicked = false;//Boolean for predefined search button clicked
    String custSearchCategory;//Category Selected for Custom Searches
    ResultSet rs;
    JTableHeader header;//table column headers
    //for exporting to .cvs
    private ResultSet exportResultSet;
    private boolean includeHeadersInCSVFile = true;

    /** Creates new form MainGui */
    public MainGui() {
        initComponents();

        //set Main Table to not be able to move columns
        header = jSearchTable.getTableHeader();
        header.setReorderingAllowed(false);

        //Set "No Search Results Found" label to be invisible on open.
        lblSearchResults.setVisible(false);
    }//MainGui() Constructor

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbSearchWithin = new javax.swing.JComboBox();
        txtKeyword = new javax.swing.JTextField();
        btCustomSearch = new javax.swing.JButton();
        cbPredefined = new javax.swing.JComboBox();
        cbFamily = new javax.swing.JComboBox();
        cbAdult = new javax.swing.JComboBox();
        cbDependent = new javax.swing.JComboBox();
        cbMinistry = new javax.swing.JComboBox();
        btViewReport = new javax.swing.JButton();
        btClear = new javax.swing.JButton();
        btEditRecord = new javax.swing.JButton();
        btExport = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator5 = new javax.swing.JSeparator();
        jSeparator6 = new javax.swing.JSeparator();
        lblTitle = new javax.swing.JLabel();
        lblSearchWithin = new javax.swing.JLabel();
        lblKeyword = new javax.swing.JLabel();
        lblGeneralReports = new javax.swing.JLabel();
        lblFamily = new javax.swing.JLabel();
        lblAdult = new javax.swing.JLabel();
        lblDependent = new javax.swing.JLabel();
        lblMinistry = new javax.swing.JLabel();
        lblSearchResults = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jSearchTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuAddNewHousehold = new javax.swing.JMenuItem();
        menuMergeHousehold = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuAddMinistry = new javax.swing.JMenuItem();
        menuReactivate = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        menuPrint = new javax.swing.JMenuItem();
        jSeparator7 = new javax.swing.JPopupMenu.Separator();
        menuExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menuAbout = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        menuUserManual = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parisioner Overseer");
        setBackground(new java.awt.Color(204, 204, 204));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        cbSearchWithin.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbSearchWithin.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Address", "Adults", "Dependents", "Families", "Ministries", "Money" }));
        cbSearchWithin.setSelectedItem("");

        txtKeyword.setFont(new java.awt.Font("Tahoma", 0, 12));

        btCustomSearch.setFont(new java.awt.Font("Tahoma", 0, 12));
        btCustomSearch.setText("Search");
        btCustomSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCustomSearchActionPerformed(evt);
            }
        });

        cbPredefined.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbPredefined.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<<Please Select a Report>>", "1. Salutation Preferred by a Particular Family", "2. A List of All Members of a Particular Family", "3. Head of Household of a Particular Family", "4. Partner of Head of Household of a Particular Family", "5. A List of All Children of a Particular Family", "6. A List of Families Associated with a Particular Child", "7. A List of Phone Numbers Belonging to a Particular Adult", "8. A List of Phone Numbers Belonging to a Particular Dependent", "9. A List of Individuals and their Phone Numbers in a Particular Family", "10. A List of Emails Belonging to a Particular Adult", "11. A List of Individuals and their Emails in a Particular Family", "12. A List of all Families Whose Year-to-Date Contribution is Less Than the Pledged Amount", "13. A List of Ministry Involvements of a Particular Adult", "14. A List of Ministry Involvements of a Particular Dependent", "15. The Date a Particular Dependent Last Helped With a Ministry", "16. The Date a Particular Adult Last Helped With a Ministry", "17. A List of Names, Emails, Phone Numbers, and Mailing Addresses of all Who Indicated Interest in a Particular Ministry", "18. A List of All Families with Dependents in Day School", "19. A List of All Families with Dependents in Religious Education" }));
        cbPredefined.setSelectedItem("");
        cbPredefined.setPreferredSize(new java.awt.Dimension(725, 21));
        cbPredefined.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                cbPredefinedPopupMenuWillBecomeVisible(evt);
            }
        });
        cbPredefined.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPredefinedActionPerformed(evt);
            }
        });

        cbFamily.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbFamily.setEnabled(false);
        cbFamily.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                cbFamilyPopupMenuWillBecomeVisible(evt);
            }
        });

        cbAdult.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbAdult.setEnabled(false);
        cbAdult.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                cbAdultPopupMenuWillBecomeVisible(evt);
            }
        });

        cbDependent.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbDependent.setEnabled(false);
        cbDependent.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                cbDependentPopupMenuWillBecomeVisible(evt);
            }
        });
        cbDependent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDependentActionPerformed(evt);
            }
        });

        cbMinistry.setFont(new java.awt.Font("Tahoma", 0, 12));
        cbMinistry.setEnabled(false);
        cbMinistry.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                cbMinistryPopupMenuWillBecomeVisible(evt);
            }
        });
        cbMinistry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbMinistryActionPerformed(evt);
            }
        });

        btViewReport.setFont(new java.awt.Font("Tahoma", 0, 12));
        btViewReport.setText("View Report");
        btViewReport.setEnabled(false);
        btViewReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViewReportActionPerformed(evt);
            }
        });

        btClear.setFont(new java.awt.Font("Tahoma", 0, 12));
        btClear.setText("Clear Results");
        btClear.setPreferredSize(new java.awt.Dimension(113, 23));
        btClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btClearActionPerformed(evt);
            }
        });

        btEditRecord.setFont(new java.awt.Font("Tahoma", 0, 12));
        btEditRecord.setText("Edit Record");
        btEditRecord.setEnabled(false);
        btEditRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditRecordActionPerformed(evt);
            }
        });

        btExport.setFont(new java.awt.Font("Tahoma", 0, 12));
        btExport.setText("Export\n");
        btExport.setEnabled(false);
        btExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExportActionPerformed(evt);
            }
        });

        jSeparator4.setForeground(new java.awt.Color(0, 153, 0));

        jSeparator6.setForeground(new java.awt.Color(0, 153, 0));

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 16));
        lblTitle.setText("Parishioner Overseer");

        lblSearchWithin.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblSearchWithin.setText("Search within:");

        lblKeyword.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblKeyword.setText("Keyword:");

        lblGeneralReports.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblGeneralReports.setText("General Reports:");

        lblFamily.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblFamily.setText("Family:");

        lblAdult.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblAdult.setText("Adult:");

        lblDependent.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblDependent.setText("Dependent:");

        lblMinistry.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblMinistry.setText("Ministry:");

        lblSearchResults.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblSearchResults.setForeground(new java.awt.Color(255, 0, 0));
        lblSearchResults.setText("No Search Results Found.");

        jSearchTable.setBackground(new java.awt.Color(230, 230, 255));
        jSearchTable.setModel(searchTableModel);
        jScrollPane1.setViewportView(jSearchTable);

        jMenu1.setText("File");

        menuAddNewHousehold.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.ALT_MASK));
        menuAddNewHousehold.setText("Add New Household...");
        menuAddNewHousehold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddNewHouseholdActionPerformed(evt);
            }
        });
        jMenu1.add(menuAddNewHousehold);

        menuMergeHousehold.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
        menuMergeHousehold.setText("Merge Existing Households...");
        menuMergeHousehold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuMergeHouseholdActionPerformed(evt);
            }
        });
        jMenu1.add(menuMergeHousehold);
        jMenu1.add(jSeparator1);

        menuAddMinistry.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK));
        menuAddMinistry.setText("Add Ministry...");
        menuAddMinistry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddMinistryActionPerformed(evt);
            }
        });
        jMenu1.add(menuAddMinistry);

        menuReactivate.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
        menuReactivate.setText("Reactivate Ministry...");
        menuReactivate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuReactivateActionPerformed(evt);
            }
        });
        jMenu1.add(menuReactivate);
        jMenu1.add(jSeparator2);

        menuPrint.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        menuPrint.setText("Print...");
        menuPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPrintActionPerformed(evt);
            }
        });
        jMenu1.add(menuPrint);
        jMenu1.add(jSeparator7);

        menuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        menuExit.setText("Exit");
        menuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuExitActionPerformed(evt);
            }
        });
        jMenu1.add(menuExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        menuAbout.setText("About this Application...");
        menuAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAboutActionPerformed(evt);
            }
        });
        jMenu2.add(menuAbout);
        jMenu2.add(jSeparator3);

        menuUserManual.setText("User Manual...");
        menuUserManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuUserManualActionPerformed(evt);
            }
        });
        jMenu2.add(menuUserManual);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 1249, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblTitle)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 303, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(btViewReport)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addGap(8, 8, 8)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jSeparator6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
                                                    .addComponent(lblGeneralReports)
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(lblDependent, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                                            .addComponent(lblFamily, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                                            .addComponent(lblAdult, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                                .addComponent(lblMinistry, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                                                .addGap(29, 29, 29)))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                            .addComponent(cbMinistry, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                            .addComponent(cbAdult, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                            .addComponent(cbFamily, 0, 271, Short.MAX_VALUE)
                                                            .addComponent(cbDependent, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                                    .addComponent(cbPredefined, 0, 441, Short.MAX_VALUE)))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(lblKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(txtKeyword, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE))
                                                    .addComponent(lblSearchWithin, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(cbSearchWithin, 0, 439, Short.MAX_VALUE)))
                                            .addComponent(btCustomSearch))
                                        .addGap(60, 60, 60)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblSearchResults, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 557, Short.MAX_VALUE))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE))))
                        .addContainerGap(10, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditRecord)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btExport, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblSearchWithin)
                        .addComponent(lblSearchResults)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbSearchWithin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblKeyword)
                            .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCustomSearch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 273, Short.MAX_VALUE)
                        .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblGeneralReports, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPredefined, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblFamily)
                            .addComponent(cbFamily, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblAdult)
                            .addComponent(cbAdult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cbDependent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDependent))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(lblMinistry))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbMinistry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btViewReport))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btEditRecord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btClear, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuAddNewHouseholdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAddNewHouseholdActionPerformed
        // TODO add your handling code here:
        AddHouseholdGui.run();
    }//GEN-LAST:event_menuAddNewHouseholdActionPerformed

    private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_menuExitActionPerformed

    private void menuAddMinistryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAddMinistryActionPerformed
        // TODO add your handling code here:
        AddMinistry.run();
    }//GEN-LAST:event_menuAddMinistryActionPerformed

    /**
     * Sets the drop-down in cbPredefined to be wider than the preferred size
     * of the combo box.
     * @param evt
     */
    private void cbPredefinedPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbPredefinedPopupMenuWillBecomeVisible
        //set width of popup menu
        setComboBoxWidth(evt);

    }//GEN-LAST:event_cbPredefinedPopupMenuWillBecomeVisible

    private void setSearchCBs(boolean cbAdultEnable, boolean cbDependentEnable, boolean cbFamilyEnable) {
        cbAdult.setEnabled(cbAdultEnable);
        cbDependent.setEnabled(cbDependentEnable);
        cbFamily.setEnabled(cbFamilyEnable);
        cbMinistry.setEnabled(false);
        btViewReport.setEnabled(true);
    }

    private void cbPredefinedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPredefinedActionPerformed

        int selectedIndex = (int) cbPredefined.getSelectedIndex();
        String combinedKey;

        switch (selectedIndex) {
            case 0: //<<Please Select a Query>>
                setSearchCBs(false, false, false);
                btViewReport.setEnabled(false);
                break;
            case 1: //Salutation Preferred by Particulat Family
                setSearchCBs(false, false, true);
                break;
            case 2: //List of All Members of a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 3: //Head of Household of a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 4: //Partner of Head of Household of a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 5: //A List of All Children of a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 6: //A List of Families Associated with a Particular Child
                setSearchCBs(false, true, false);
                break;
            case 7: //A List of Phone Numbers Belonging to a Particular Adult
                setSearchCBs(true, false, false);
                break;
            case 8: //A List of Phone Numbers Belonging to a Particular Dependent
                setSearchCBs(false, true, false);
                break;
            case 9: //A List of Indiviuals and Their Phone Numbers in a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 10: //A List of Emails Belonging to a Particular Adult
                setSearchCBs(true, false, false);
                break;
            case 11: //A List of Individuals and Their Emails in a Particular Family
                setSearchCBs(false, false, true);
                break;
            case 12: //A List of all Families Whose Year-to-Date Contribution is Less Than the Pledged Amount
                setSearchCBs(false, false, false);
                break;
            case 13: //A List of Ministry Involvements of a Particular Adult
                setSearchCBs(true, false, false);
                break;
            case 14: //A List of Ministry Involvements of a Particular Dependent
                setSearchCBs(false, true, false);
                break;
            case 15: //The Date a Particular Dependent Last Helped With a Ministry
                setSearchCBs(false, true, false);
                break;
            case 16: //The Date a Particular Adult Last Helped With a Ministry
                setSearchCBs(true, false, false);
                break;
            case 17: //A List of Names, Emails, Phone Numbers, and Mailing Addresses of all Parishioners Who Indicated Interest in a Particular Ministry
                setSearchCBs(false, false, false);
                cbMinistry.setEnabled(true);
                break;
            case 18: //A List of All Families with Dependents in Day School
                setSearchCBs(false, false, false);
                break;
            case 19: //A List of All Families with Dependents in Religious Ed
                setSearchCBs(false, false, false);
                break;
        }//switch
    }//GEN-LAST:event_cbPredefinedActionPerformed

    private void btEditRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditRecordActionPerformed
        int rowIndex = jSearchTable.getSelectedRow();

        if (rowIndex >= 0) {
            editRecord = new Object[searchTableModel.getColumnCount()];

            for (int i = 0; i < searchTableModel.getColumnCount(); i++) {
                editRecord[i] = searchTableModel.getValueAt(rowIndex, i);
            }

            try {
                DBManager dbManager = new DBManager();
                if (searchWithinClicked) {
                    Object cbItem = cbSearchWithin.getSelectedItem();
                    if ((custSearchCategory.equals("Address"))
                          | (custSearchCategory.equals("Families"))
                          | (custSearchCategory.equals("Money"))){
                        //Open Family Record
                        Family family = dbManager.getFamily((String) editRecord[0], (String) editRecord[1]);
                        Object[] familyAttribsObj = family.getAttributes();
                        String[] familyAttribs = new String[familyAttribsObj.length];
                        familyAttribs[0] = familyAttribsObj[0] + "";
                        familyAttribs[1] = familyAttribsObj[1] + "";
                        familyAttribs[2] = familyAttribsObj[2] + "";
                        familyAttribs[3] = familyAttribsObj[3] + "";
                        familyAttribs[4] = familyAttribsObj[4] + "";
                        familyAttribs[5] = familyAttribsObj[5] + "";
                        familyAttribs[6] = familyAttribsObj[6] + "";

                        EditHouseholdGui.run(familyAttribs);
                    }else if (custSearchCategory.equals("Adults")) {
                        Adult adult = (Adult) dbManager.getIndividual((String) editRecord[0],
                                (String) editRecord[1],
                                (String) editRecord[2]);
                        String[] adultKeyArray = (String[]) adult.getAttributes();

                        EditAdult.run(adultKeyArray);
                    }//elseIf-Adult
                    else if (custSearchCategory.equals("Dependents")) {
                        Dependent dependent = (Dependent) dbManager.getIndividual((String) editRecord[0],
                                (String) editRecord[1],
                                (String) editRecord[2]);
                        String[] dependentKeyArray = (String[]) dependent.getAttributes();

                        EditDependent.run(dependentKeyArray);
                    }//elseIf-Dep
                    else if (custSearchCategory.equals("Ministries")) {
                        String[] ministry = dbManager.getMinistry((String) editRecord[0]);
                        EditMinistry.run(ministry);

                    }//elseIf-Ministries
                }//if(searchwithin)

            }//try
            catch (Exception ex) {
                ex.getCause();
                ex.getMessage();
                ex.toString();
                JOptionPane.showMessageDialog(null, "An error has occurred while editing this record." + "\n"
                        + "Please contact your Database Administrator, a.k.a Thomas Naps, for help."
                        + "\n" + "(btEditRecordActionPerformed(evt) Method)",
                        "Database Error", JOptionPane.ERROR_MESSAGE);
            }//catch
        }//if
    }//GEN-LAST:event_btEditRecordActionPerformed

    private void btCustomSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCustomSearchActionPerformed
        custSearchCategory = cbSearchWithin.getSelectedItem().toString();
        searchWithinClicked = true;
        searchPredefinedClicked = false;
        searchTableModel.setRowCount(1);
        btEditRecord.setEnabled(true);
        btExport.setEnabled(true);

        ResultSet rs = null;
        Object searchItem = cbSearchWithin.getSelectedItem();
        String keyword = txtKeyword.getText();
        try {
            DBQuery dbQuerier = new DBQuery();
            if (searchItem.equals("Address")) {
                rs = dbQuerier.generalQuery("Address", keyword);
                //for exporting to .csv
                exportResultSet = dbQuerier.generalQuery("Address", keyword);
            }//if
            else if (searchItem.equals("Adults")) {
                rs = dbQuerier.generalQuery("Adults", keyword);
                exportResultSet = dbQuerier.generalQuery("Adults", keyword);
            }//else-if
            else if (searchItem.equals("Dependents")) {
                rs = dbQuerier.generalQuery("Dependents", keyword);
                exportResultSet = dbQuerier.generalQuery("Dependents", keyword);
            }//else-if
            else if (searchItem.equals("Families")) {
                rs = dbQuerier.generalQuery("Families", keyword);
                exportResultSet = dbQuerier.generalQuery("Families", keyword);
            }//else-if
            else if (searchItem.equals("Ministries")) {
                rs = dbQuerier.generalQuery("Ministries", keyword);
                exportResultSet = dbQuerier.generalQuery("Ministries", keyword);
            }//else-if
            else if (searchItem.equals("Money")) {
                rs = dbQuerier.generalQuery("Money", keyword);
                exportResultSet = dbQuerier.generalQuery("Money", keyword);
            }//else-if
            displayResultSet(rs);
        }//try
        catch (Exception ex) {
            System.err.println("Exception in Custom Search Action Performed: " + "\n"
                    + ex.getMessage());
        }//catch

    }//GEN-LAST:event_btCustomSearchActionPerformed

    private void btClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btClearActionPerformed
        //reset booleans
        searchWithinClicked = false;
        searchPredefinedClicked = false;
        btExport.setEnabled(false);
        btEditRecord.setEnabled(false);

        //reset Search Results Label
        lblSearchResults.setVisible(false);

        //reset table
        searchTableModel.setRowCount(1);
        for (int i = 0; i < jSearchTable.getColumnCount(); i++) {
            jSearchTable.setValueAt("", 0, i);
        }
        //reset table header

        for (int m = 0; m < jSearchTable.getColumnCount(); m++) {
            jSearchTable.getColumnModel().getColumn(m).setHeaderValue("");
        }
        header = jSearchTable.getTableHeader();
        header.repaint();

        //reset drop-downs & textfields
        //cbSearchWithin.setSelectedIndex(0);
        //cbPredefined.setSelectedIndex(0);
        //cbFamily.setSelectedIndex(0);
        cbFamily.setEnabled(false);
        //cbAdult.setSelectedIndex(0);
        cbAdult.setEnabled(false);
        //cbDependent.setSelectedIndex(0);
        cbDependent.setEnabled(false);
        txtKeyword.setText("");

    }//GEN-LAST:event_btClearActionPerformed

    private void btViewReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViewReportActionPerformed
        //Set category to be empty when Predefined Search Button is Clciked
        custSearchCategory = "";


        searchWithinClicked = false;
        searchPredefinedClicked = true;
        searchTableModel.setRowCount(1);
        btEditRecord.setEnabled(false);
        btExport.setEnabled(true);

        ResultSet rs = null;
        int predefinedQueryNumber = (int) cbPredefined.getSelectedIndex();
        String[] familyKey = null;
        String[] adultKey = null;
        String[] dependentKey = null;
        String ministryName = null;

        try {
            DBQuery dbQuerier = new DBQuery();
            if (cbFamily.isEnabled()) {
                familyKey = ((String) cbFamily.getSelectedItem()).split(";");
            }//if-Fam
            if (cbAdult.isEnabled()) {
                adultKey = ((String) cbAdult.getSelectedItem()).split(";");
            }//if-Adult
            if (cbDependent.isEnabled()) {
                dependentKey = ((String) cbDependent.getSelectedItem()).split(";");
            }//if-Dep
            if (cbMinistry.isEnabled()) {
                ministryName = (String) cbMinistry.getSelectedItem();
            }//if-Ministry

            switch (predefinedQueryNumber) {
                case 1: //Salutation Preferred by Particulat Family
                    rs = dbQuerier.predefinedQuery01(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery01(familyKey[0], familyKey[1]);
                    break;
                case 2: //List of All Members of a Particular Family
                    rs = dbQuerier.predefinedQuery02(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery02(familyKey[0], familyKey[1]);
                    break;
                case 3: //Head of Household of a Particular Family
                    rs = dbQuerier.predefinedQuery03(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery03(familyKey[0], familyKey[1]);
                    break;
                case 4: //Partner of Head of Household of a Particular Family
                    rs = dbQuerier.predefinedQuery04(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery04(familyKey[0], familyKey[1]);
                    break;
                case 5: //A List of All Children of a Particular Family
                    rs = dbQuerier.predefinedQuery05(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery05(familyKey[0], familyKey[1]);
                    break;
                case 6: //A List of Families Associated with a Particular Child
                    rs = dbQuerier.predefinedQuery06(dependentKey[0], dependentKey[1], dependentKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery06(dependentKey[0], dependentKey[1], dependentKey[2]);
                    break;
                case 7: //A List of Phone Numbers Belonging to a Particular Adult
                    rs = dbQuerier.predefinedQuery07(adultKey[0], adultKey[1], adultKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery07(adultKey[0], adultKey[1], adultKey[2]);
                    break;
                case 8: //A List of Phone Numbers Belonging to a Particular Dependent
                    rs = dbQuerier.predefinedQuery08(dependentKey[0], dependentKey[1], dependentKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery08(dependentKey[0], dependentKey[1], dependentKey[2]);
                    break;
                case 9: //A List of Indiviuals and Their Phone Numbers in a Particular Family
                    rs = dbQuerier.predefinedQuery09(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery09(familyKey[0], familyKey[1]);
                    break;
                case 10: //A List of Emails Belonging to a Particular Adult
                    rs = dbQuerier.predefinedQuery10(adultKey[0], adultKey[1], adultKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery10(adultKey[0], adultKey[1], adultKey[2]);
                    break;
                case 11: //A List of Individuals and Their Emails in a Particular Family
                    rs = dbQuerier.predefinedQuery11(familyKey[0], familyKey[1]);
                    exportResultSet = dbQuerier.predefinedQuery11(familyKey[0], familyKey[1]);
                    break;
                case 12: //A List of all Families Whose Year-to-Date Contribution is Less Than the Pledged Amount
                    rs = dbQuerier.predefinedQuery12();
                    exportResultSet = dbQuerier.predefinedQuery12();
                    break;
                case 13: //A List of Ministry Involvements of a Particular Adult
                    rs = dbQuerier.predefinedQuery13(adultKey[0], adultKey[1], adultKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery13(adultKey[0], adultKey[1], adultKey[2]);
                    break;
                case 14: //A List of Ministry Involvements of a Particular Dependent
                    rs = dbQuerier.predefinedQuery14(dependentKey[0], dependentKey[1], dependentKey[2]);
                   exportResultSet = dbQuerier.predefinedQuery14(dependentKey[0], dependentKey[1], dependentKey[2]);
                    break;
                case 15: //The Date a Particular Dependent Last Helped With a Ministry
                    rs = dbQuerier.predefinedQuery15(dependentKey[0], dependentKey[1], dependentKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery15(dependentKey[0], dependentKey[1], dependentKey[2]);
                    break;
                case 16: //The Date a Particular Adult Last Helped With a Ministry
                    rs = dbQuerier.predefinedQuery16(adultKey[0], adultKey[1], adultKey[2]);
                    exportResultSet = dbQuerier.predefinedQuery16(adultKey[0], adultKey[1], adultKey[2]);
                    break;
                case 17: //A List of Names, Emails, Phone Numbers, and Mailing Addresses of all Parishioners Who Indicated Interest in a Particular Ministry
                    rs = dbQuerier.predefinedQuery17(ministryName);
                    exportResultSet = dbQuerier.predefinedQuery17(ministryName);
                    break;
                case 18: //A List of All Families with Dependents in Day School
                    rs = dbQuerier.predefinedQuery18();
                    exportResultSet = dbQuerier.predefinedQuery18();
                    break;
                case 19: //A List of All Families with Dependents in Religious Ed
                    rs = dbQuerier.predefinedQuery19();
                    exportResultSet = dbQuerier.predefinedQuery19();
                    break;
            }//switch

            displayResultSet(rs);

        }//try
        catch (Exception ex) {
        }//catch
    }//GEN-LAST:event_btViewReportActionPerformed

    private void btExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExportActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter csvFileFilter = new FileNameExtensionFilter("Comma-seperated value files (*.csv)", ".csv");
        fileChooser.addChoosableFileFilter(csvFileFilter);

        int saveResult = fileChooser.showSaveDialog(this);
        if (saveResult != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File file = fileChooser.getSelectedFile();
        String filename = file.getAbsolutePath() + ".csv";

        try {
            CSVWriter writer = new CSVWriter(new FileWriter(filename));
            writer.writeAll(exportResultSet, includeHeadersInCSVFile);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            //IOException & SQLException
            System.err.println("Error when saving file: " + e);
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Sorry, error occured in saving data to .csv file", "Unsucessful Save", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btExportActionPerformed

    private void cbFamilyPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbFamilyPopupMenuWillBecomeVisible
        //set width of popup menu
        setComboBoxWidth(evt);

    }//GEN-LAST:event_cbFamilyPopupMenuWillBecomeVisible

    private void cbAdultPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbAdultPopupMenuWillBecomeVisible
        //set width of popup menu
        setComboBoxWidth(evt);
    }//GEN-LAST:event_cbAdultPopupMenuWillBecomeVisible

    private void cbDependentPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbDependentPopupMenuWillBecomeVisible
        //set width of popup menu
        setComboBoxWidth(evt);

    }//GEN-LAST:event_cbDependentPopupMenuWillBecomeVisible

    private void menuUserManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuUserManualActionPerformed
        //open user manual
        try {
            Desktop.getDesktop().open(new File("UserManual.pdf"));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "'UserManual.pdf' could not be found. Sorry for any inconvenience.",
                    "File Not Found Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_menuUserManualActionPerformed

    private void menuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAboutActionPerformed
        //About this program popup
        ImageIcon icon = new ImageIcon("src/resources/heart.png");
        JOptionPane.showMessageDialog(null, "Parishioner Overseer\n"
                + "Version 1.0\n"
                + "Created by "
                + "Gabrielle DuBois and "
                + "Sarah Wegner.", "About This Application", 3, icon);
    }//GEN-LAST:event_menuAboutActionPerformed

    private void menuReactivateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuReactivateActionPerformed
        ReactivateMinistry.run();
    }//GEN-LAST:event_menuReactivateActionPerformed

    private void menuMergeHouseholdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuMergeHouseholdActionPerformed
        MergeExistingHouseholds.run();
    }//GEN-LAST:event_menuMergeHouseholdActionPerformed

    private void cbMinistryPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbMinistryPopupMenuWillBecomeVisible
        // TODO add your handling code here:
    }//GEN-LAST:event_cbMinistryPopupMenuWillBecomeVisible

    private void cbMinistryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbMinistryActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbMinistryActionPerformed

    private void cbDependentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDependentActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbDependentActionPerformed

    private void menuPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPrintActionPerformed
        // CODE TO PRINT. MOVE TO MENU OPTION
        //Export Table Contents to print on printer
        try {
            jSearchTable.print();
        }//try
        catch (PrinterException e) {
            JOptionPane.showMessageDialog(null, "An error has occurred while printing." + "\n"
                    + "Please make sure there are search results or generated reports to print in table before printing.",
                    "Printing Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//GEN-LAST:event_menuPrintActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus

        //if the custom search button was clicked, when the window gains
        //focus, update the information
        if(searchWithinClicked){
            refreshSearchResults();
        }//if

        //reset each report drop-down
        try {
            DBManager dbManager = new DBManager();

            // set Dependent drop-down queryfrom DB
            ArrayList<String> depKeys = dbManager.getAllIndividualKeys("Dependents");
            for (int i = 0; i < depKeys.size(); i++) {
                cbDependent.addItem(depKeys.get(i));
            }//for i

            // set Adults drop-down queryfrom DB
            ArrayList<String> adultKeys = dbManager.getAllIndividualKeys("Adults");
            for (int i = 0; i < depKeys.size(); i++) {
                cbAdult.addItem(adultKeys.get(i));
            }
            // set Family drop-down queryfrom DB
            ArrayList<String> famKeys = dbManager.getAllFamilyKeys();
            for (int i = 0; i < famKeys.size(); i++) {
                cbFamily.addItem(famKeys.get(i));
            }
            // set Minitry drop-down queryfrom DB
            ArrayList<String> minitryNames = dbManager.getMinistryNames(true);
            for (int i = 0; i < minitryNames.size(); i++) {
                cbMinistry.addItem(minitryNames.get(i));
            }
        }//try
        catch (Exception e) {

        }//catch
    }//GEN-LAST:event_formWindowGainedFocus

    /**
     * Refresh the Search Window after another window is closes
     */
    public void refreshSearchResults(){
        java.awt.event.ActionEvent evt = null;
        btCustomSearchActionPerformed(evt);
    }//refreshSearchResults()

    /**
     * Displays the result set from searches to the search table
     * @param ResultSet - result set returned from general query
     * in Custom Search
     */
    public void displayResultSet(ResultSet rs) {
        if (rs == null) {
            lblSearchResults.setVisible(true);
            //no exporting with empty results
            btExport.setEnabled(false);
        } else {
            try {
                if (rs.next()) {
                    lblSearchResults.setVisible(false);
                    int numColumns = rs.getMetaData().getColumnCount();

                    Object[] columnIdentifiers = new Object[numColumns];
                    Object[] rowData = new Object[numColumns];

                    //get column names into array
                    for (int j = 1; j <= numColumns; j++) {
                        columnIdentifiers[j - 1] = rs.getMetaData().getColumnName(j);
                    }//for-j

                    do {
                        for (int i = 1; i <= numColumns; i++) {
                            rowData[i - 1] = rs.getObject(i);
                        }//for-i
                        searchTableModel.addRow(rowData);
                    } while (rs.next());
                    jSearchTable.setModel(searchTableModel);

                    for (int m = 0; m < columnIdentifiers.length; m++) {
                        jSearchTable.getColumnModel().getColumn(m).setHeaderValue(columnIdentifiers[m]);
                    }//for-m
                    for (int m = columnIdentifiers.length; m < jSearchTable.getColumnCount(); m++) {
                        jSearchTable.getColumnModel().getColumn(m).setHeaderValue(" ");
                    }//for-m
                    //display table columns
                    header = jSearchTable.getTableHeader();
                    header.setReorderingAllowed(false);
                    header.repaint();
                }//if
                else {
                    lblSearchResults.setVisible(true);
                    //no exporting with empty results
                    btExport.setEnabled(false);
                }
            } catch (Exception e) {
                System.err.println("exception: " + e.getMessage());
                e.getCause();
                e.printStackTrace();
            }
        }//else (rs != null)
    }//displayResultSet

    /**
     * This method will set the width of a
     * pop-up menu of a combo box
     */
    public void setComboBoxWidth(javax.swing.event.PopupMenuEvent evt) {
        JComboBox box = (JComboBox) evt.getSource();
        Object comp = box.getUI().getAccessibleChild(box, 0);
        if (!(comp instanceof JPopupMenu)) {
            return;
        }
        JComponent scrollPane = (JComponent) ((JPopupMenu) comp).getComponent(0);
        Dimension size = new Dimension();
        size.width = box.getPreferredSize().width;
        size.height = scrollPane.getPreferredSize().height;
        scrollPane.setPreferredSize(size);
        scrollPane.setMaximumSize(size);
    }//setComboBoxWidth

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGui().setVisible(true);
            }
        });
    }//main

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClear;
    private javax.swing.JButton btCustomSearch;
    private javax.swing.JButton btEditRecord;
    private javax.swing.JButton btExport;
    private javax.swing.JButton btViewReport;
    private javax.swing.JComboBox cbAdult;
    private javax.swing.JComboBox cbDependent;
    private javax.swing.JComboBox cbFamily;
    private javax.swing.JComboBox cbMinistry;
    private javax.swing.JComboBox cbPredefined;
    private javax.swing.JComboBox cbSearchWithin;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jSearchTable;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JPopupMenu.Separator jSeparator7;
    private javax.swing.JLabel lblAdult;
    private javax.swing.JLabel lblDependent;
    private javax.swing.JLabel lblFamily;
    private javax.swing.JLabel lblGeneralReports;
    private javax.swing.JLabel lblKeyword;
    private javax.swing.JLabel lblMinistry;
    private javax.swing.JLabel lblSearchResults;
    private javax.swing.JLabel lblSearchWithin;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JMenuItem menuAbout;
    private javax.swing.JMenuItem menuAddMinistry;
    private javax.swing.JMenuItem menuAddNewHousehold;
    private javax.swing.JMenuItem menuExit;
    private javax.swing.JMenuItem menuMergeHousehold;
    private javax.swing.JMenuItem menuPrint;
    private javax.swing.JMenuItem menuReactivate;
    private javax.swing.JMenuItem menuUserManual;
    private javax.swing.JTextField txtKeyword;
    // End of variables declaration//GEN-END:variables
    private MainTableModel searchTableModel = new MainTableModel(
            new Object[][]{
                {"", "", "", "", "", "", "", "", "", "",""}
            },
            new String[]{
                " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "});
}//MainGui Class
